-? 0 value xt
0 value #1
0 value #2
0 value #3
0 value #4

:[] file:[  ( addr count xt -- ) ( size handle -- )
  to xt  2dup file-exists not if
    W/O create-file throw >q
  else
    R/W open-file throw >q
  then
  q@ file-size drop drop  q@  xt catch
  q> close-file drop
  throw ;

\ Regardless of the size of the file, this will resize it to 0
\  Must be used right after FILE:[
: empty-file   ( size fileid -- fileid )
   0 0 third resize-file drop nip ;

:[] read-file:[  ( addr count xt -- ) ( addr size -- )
  to xt R/O open-file throw >q
  q@ map-file throw 2dup xt catch >r
  q> close-file drop
  .s unmap-file throw
  r> throw ;


0 Value #out

Create crlf 2 c, 13 c, 10 c,
Variable wbuf
: write   ( addr c -- ) #out write-file drop ;
: h^   ( n -- ) wbuf h! wbuf 2 write ;
: ^   ( n -- ) wbuf ! wbuf 4 write ;

: (femit)   here c! here 1 write ;
: (fcr)   crlf count write ;

create File-Out
   4 cells ,
   7 ,
   0 ,
   0 ,
   ' noop ,
   ' noop ,
   ' noop ,
   ' (femit) ,
   ' write ,
   ' write ,
   ' (fcr) ,

-? 0 value xt
:[] file-out:[  ( addr count xt -- ) ( -- )
   to xt
   file:[
      empty-file to #out
      file-out open-personality
      xt catch
      close-personality
      throw
   ;] ;

: cache-file,
   file:[ over , here -rot read-file throw allot ;] ;

: cache-count
  @+ ;

: >ext ( addr c -- addr c )   \ format of output is ".ext"
   2dup -EXT +  ( start count ext )
   -rot + over - ;

create buf$ 256 allot
\ GET-LINE returns false if no more lines
: get-line ( fileid -- addr c flag )
   >r buf$ dup 256 r> read-line drop ;
